import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.12

Rectangle {
    id: mainWindow
    width: 1400
    height: 900
    color: "#f5f5f5"
    
    // 信号定义
    signal logoutRequested()
    signal toggleFullscreen()  // 添加全屏切换信号

    // 全局属性
    property string currentPage: "操作首页"
    property string currentRobot: "GZJY001"
    property string currentDateTime: ""
    property string currentWeather: "25℃，50%RH，晴"
    property bool showRobotList: false
    property bool isFullscreen: false  // 添加全屏状态属性
    property var currentRobotData: null  // 当前机器人实时数据
    
    
    // 样式常量
    readonly property color primaryColor: "white"
    readonly property color secondaryColor: "#27ae60"
    readonly property color successColor: "#27ae60"
    readonly property color warningColor: "#f39c12"
    readonly property color errorColor: "#e74c3c"
    readonly property color backgroundColor: "#f5f5f5"
    readonly property color cardBackground: "#286faf"
    readonly property color textColor: "#2c3e50"
    readonly property color lightTextColor: "#7f8c8d"
    
    // 尺寸常量
    readonly property int borderRadius: 8
    readonly property int smallBorderRadius: 4
    readonly property int shadowRadius: 8
    readonly property int cardMargin: 15
    
    // 确保焦点可以被接收以处理键盘事件
    focus: true
    
    // 可重用的状态卡片组件
    Component {
        id: statusCardComponent
        
        Rectangle {
            property string title: ""
            property var dataModel: []
            property bool cardVisible: true
            
            color: cardBackground
            radius: borderRadius
            Layout.fillWidth: true
            visible: cardVisible
            
            DropShadow {
                anchors.fill: parent
                source: parent
                horizontalOffset: 0
                verticalOffset: 1
                radius: 4
                samples: 8
                color: "#10000000"
            }
            
            ColumnLayout {
                anchors.fill: parent
                spacing: 0
                
                // 标题栏
                Rectangle {
                    color: "#f8f9fa"
                    Layout.fillWidth: true
                    Layout.preferredHeight: 35
                    radius: borderRadius
                    
                    Label {
                        text: parent.parent.parent.title
                        font.pixelSize: 14
                        font.bold: true
                        color: textColor
                        anchors.left: parent.left
                        anchors.leftMargin: cardMargin
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }
                
                // 内容区域
                ColumnLayout {
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    anchors.margins: cardMargin
                    spacing: 8
                    
                    Repeater {
                        model: parent.parent.parent.dataModel
                        
                        RowLayout {
                            Layout.fillWidth: true
                            Layout.preferredHeight: 25
                            spacing: 10
                            
                            Label {
                                text: modelData.label + ":"
                                color: secondaryColor
                                font.pixelSize: 12
                                width: 80
                            }
                            
                            Rectangle {
                                width: 6
                                height: 6
                                radius: 3
                                color: {
                                    switch(modelData.status) {
                                        case "normal": return successColor
                                        case "warning": return warningColor
                                        case "error": return errorColor
                                        case "info": return primaryColor
                                        default: return lightTextColor
                                    }
                                }
                            }
                            
                            Label {
                                text: modelData.value
                                color: textColor
                                font.pixelSize: 12
                                font.bold: modelData.status === "normal"
                                Layout.fillWidth: true
                            }
                        }
                    }
                }
            }
        }
    }

    // 当前机器人是否离线（全局属性，带安全检查）
    property bool isCurrentRobotOffline: {
        if (!currentRobot || !robotList || robotList.length === 0) {
            return true  // 默认情况下视为离线
        }
        return !getRobotOnlineStatus(currentRobot)
    }
    
    // 机器人数据模型（从数据库动态获取）
    property var robotList: []
    
    // 机器人管理器连接
    Connections {
        target: typeof robotManager !== 'undefined' ? robotManager : null
        
        onRobotRegistered: function(sn) {
            console.log("机器人注册:", sn)
            refreshRobotList()
        }
        
        onRobotDisconnected: function(sn) {
            console.log("机器人断开:", sn)
            refreshRobotList()
        }
        
        onRobotDataReceived: function(sn, data) {
            console.log("机器人数据更新:", sn, JSON.stringify(data))
            // 实时更新当前机器人数据
            if (sn === currentRobot) {
                updateCurrentRobotData(data)
            }
            // 刷新机器人列表以更新状态
            refreshRobotList()
        }
        
        onServerRunningChanged: {
            if (robotManager.serverRunning) {
                refreshRobotList()
            }
        }
    }

    // 监听用户权限变化，刷新主页
    Connections {
        target: typeof authManager !== 'undefined' ? authManager : null
        
        onLoginStatusChanged: {
            // 当登录状态改变时，强制重新加载主页
            if (homePageLoader && homePageLoader.active) {
                homePageLoader.active = false
                homePageReloadTimer.start()
            }
        }
    }
    
    // 主页重载定时器
    Timer {
        id: homePageReloadTimer
        interval: 50
        onTriggered: {
            homePageLoader.active = true
        }
    }
    
    // 初始化
    Component.onCompleted: {
        updateDateTime()
        timer.start()
        
        // 初始化机器人列表
        refreshRobotList()
        
        // 检查是否有可用的机器人作为默认选择
        if (robotList.length > 0 && !currentRobot) {
            currentRobot = robotList[0].sn
        }
    }

    // 定时器用于更新时间
    Timer {
        id: timer
        interval: 1000
        running: false
        triggeredOnStart: true
        onTriggered: updateDateTime()
    }
    
    // 机器人列表刷新定时器
    Timer {
        id: robotRefreshTimer
        interval: 10000  // 10秒刷新一次
        running: true
        repeat: true
        onTriggered: {
            if (typeof robotManager !== 'undefined' && robotManager.serverRunning) {
                refreshRobotList()
            }
        }
    }

    // 更新日期时间函数
    function updateDateTime() {
        const now = new Date()
        const year = now.getFullYear()
        const month = String(now.getMonth() + 1).padStart(2, '0')
        const day = String(now.getDate()).padStart(2, '0')
        const hours = String(now.getHours()).padStart(2, '0')
        const minutes = String(now.getMinutes()).padStart(2, '0')
        const seconds = String(now.getSeconds()).padStart(2, '0')
        currentDateTime = `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`
    }

    // 切换页面函数
    function switchPage(pageName) {
        currentPage = pageName
        // 更新导航按钮状态
        for (let i = 0; i < navButtons.count; i++) {
            navButtons.itemAt(i).isSelected = (navButtons.itemAt(i).pageName === pageName)
        }
        
        // 如果切换到远程控制页面，调用页面的激活函数
        if (pageName === "远程控制") {
            // 使用定时器确保页面加载完成后再调用激活函数
            pageLoaderTimer.restart()
        }
    }
    
    // 页面加载器定时器，用于在页面加载完成后调用激活函数
    Timer {
        id: pageLoaderTimer
        interval: 100  // 100毫秒后调用
        repeat: false
        onTriggered: {
            // 检查远程控制页面是否已加载并调用激活函数
            if (remoteControlPageLoader.status === Loader.Ready && remoteControlPageLoader.item && 
                typeof remoteControlPageLoader.item.onPageActivated === "function") {
                remoteControlPageLoader.item.onPageActivated()
            }
        }
    }

    // 切换机器人函数（融合代码2的简化逻辑，但保留代码1的状态检查）
    function switchRobot(robotSN) {
        if (currentRobot !== robotSN) {
            currentRobot = robotSN
            updateRobotStatus()
            console.log("切换到机器人：" + robotSN + "，在线状态：" + getRobotOnlineStatus(robotSN))
        }
    }
    
    // 刷新机器人列表函数（适配内存优先架构）
    function refreshRobotList() {
        if (typeof robotManager !== 'undefined') {
            try {
                var robots = robotManager.getAllRobots()
                var newRobotList = []
                
                for (var i = 0; i < robots.length; i++) {
                    var robot = robots[i]
                    // 正确处理内存优先架构的数据结构
                    // 内存缓存中的数据结构：{sn, online, data, last_update, last_ping}
                    var robotData = {
                        name: robot.sn || robot.name,
                        sn: robot.sn,
                        isOnline: robot.online || false,  // 直接使用online字段
                        online: robot.online || false,    // 直接使用online字段
                        ip: robot.ip || "",
                        port: robot.port || 0,
                        conn_time: robot.conn_time || "",
                        last_ping: robot.last_ping || "",
                        id: robot.id || i
                    }
                    
                    // 如果有data字段（来自内存缓存），提取实时数据
                    if (robot.data) {
                        robotData.position = robot.data.position || robot.data.pos_x !== undefined ? {
                            x: robot.data.pos_x || 0,
                            y: robot.data.pos_y || 0,
                            z: robot.data.pos_z || 0
                        } : null
                        robotData.temperature = robot.data.temperature || robot.data.temp || 0
                        robotData.battery = robot.data.battery || 0
                        robotData.status = robot.data.status || "unknown"
                    }
                    
                    newRobotList.push(robotData)
                }
                
                robotList = newRobotList
                console.log("机器人列表已更新（内存优先），共", robotList.length, "个机器人")
                
                // 打印详细的机器人状态信息
                for (var j = 0; j < robotList.length; j++) {
                    console.log("机器人", robotList[j].sn, "在线状态:", robotList[j].online)
                    // 调试：打印完整的机器人数据结构
                    console.log("机器人完整数据:", JSON.stringify(robotList[j]))
                }
                
                // 如果当前机器人不在列表中，选择第一个可用的
                if (!getRobotByName(currentRobot) && robotList.length > 0) {
                    currentRobot = robotList[0].sn
                }
            } catch (error) {
                console.log("获取机器人列表失败:", error)
                // 如果获取失败，使用默认列表
                robotList = [
                    { name: "GZJY001", sn: "GZJY001", isOnline: false, online: false },
                    { name: "GZJY002", sn: "GZJY002", isOnline: false, online: false }
                ]
            }
        } else {
            console.log("robotManager 不可用，使用默认机器人列表")
            // 如果robotManager不可用，使用默认列表
            robotList = [
                { name: "GZJY001", sn: "GZJY001", isOnline: false, online: false },
                { name: "GZJY002", sn: "GZJY002", isOnline: false, online: false }
            ]
        }
    }
    
    // 根据名称获取机器人信息
    function getRobotByName(robotName) {
        for (var i = 0; i < robotList.length; i++) {
            if (robotList[i].name === robotName || robotList[i].sn === robotName) {
                return robotList[i]
            }
        }
        return null
    }
    
    // 更新当前机器人实时数据
    function updateCurrentRobotData(data) {
        if (!data) return
        
        // 初始化currentRobotData对象
        if (!currentRobotData) {
            currentRobotData = {}
        }
        
        // 更新位置信息
        if (data.position) {
            currentRobotData.position = data.position
        }
        
        // 更新温度信息
        if (data.temperature !== undefined) {
            currentRobotData.temperature = data.temperature
        }
        
        // 更新电量信息
        if (data.battery !== undefined) {
            currentRobotData.battery = data.battery
        }
        
        // 更新状态信息
        if (data.status) {
            currentRobotData.status = data.status
        }
        
        console.log("更新当前机器人实时数据:", JSON.stringify(currentRobotData))
    }

    // 顶部信息栏（整合代码2的用户信息和退出登录功能）
    Rectangle {
        id: topBar
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        height: 70
        color: secondaryColor

        // 添加阴影效果
        DropShadow {
            anchors.fill: parent
            source: parent
            horizontalOffset: 0
            verticalOffset: 2
            radius: shadowRadius
            samples: 16
            color: "#40000000"
        }

        RowLayout {
            anchors.fill: parent
            anchors.margins: 15
            spacing: 20

            // 平台名称（保留代码1）
            Label {
                text: "甘蔗农机智能管控平台 V1.0"
                font.pixelSize: 22
                font.bold: true
                color: "white"
                Layout.alignment: Qt.AlignLeft
            }

            // 中间区域（整合用户信息、机器人选择、时间天气）
            RowLayout {
                Layout.fillWidth: true
                spacing: 15
                Layout.alignment: Qt.AlignVCenter

                // ------------------------ 代码2新增：用户信息区域 ------------------------
                RowLayout {
                    spacing: 8

                    Label {
                        text: "当前用户："
                        color: "#bdc3c7"
                        font.pixelSize: 14
                    }

                    // 动态显示当前用户（依赖authManager）
                    Label {
                        text: typeof authManager !== 'undefined' ? authManager.currentUser : "未登录"
                        color: "#ffffff"
                        font.pixelSize: 14
                        font.bold: true
                    }

                    // 退出登录按钮（仅当用户已登录时可见）
                    Button {
                        text: "退出登录"
                        Layout.preferredWidth: 80
                        Layout.preferredHeight: 28

                        // 条件可见性：仅当authManager存在且用户已登录时显示
                        visible: typeof authManager !== 'undefined' ? authManager.isLoggedIn : false

                        background: Rectangle {
                            color: errorColor
                            radius: smallBorderRadius
                            border.color: "#c0392b"
                            border.width: 1
                        }

                        contentItem: Text {
                            text: "退出登录"
                            color: "white"
                            font.pixelSize: 11
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                        }

                        onClicked: {
                            if (typeof authManager !== 'undefined') {
                                authManager.logoutUser()  // 调用认证管理器的退出方法
                                mainWindow.logoutRequested()  // 发出退出登录信号
                            }
                        }
                    }
                }

                // 分隔线（代码2风格）

                // ------------------------ 代码2新增：机器人快速选择下拉框 ------------------------

                // ------------------------ 代码1原有：时间与天气显示 ------------------------
                RowLayout {
                    spacing: 15
                    Layout.alignment: Qt.AlignVCenter
                    Layout.fillWidth: true // 确保RowLayout填满可用宽度

                    // 占位Item，推动后面的内容向右
                    Item {
                        Layout.fillWidth: true
                    }

                    // 时间显示
                    Label {
                        text: "时间：" + currentDateTime
                        color: "#bdc3c7"
                        font.pixelSize: 14
                    }

                    // 天气显示
                    Label {
                        text: "天气：" + currentWeather
                        color: "#bdc3c7"
                        font.pixelSize: 14
                    }
                }
            }
        }
    }

    // 主内容区（完全保留代码1结构）
    Rectangle {
        anchors.top: topBar.bottom
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        color: "#f5f5f5"

        RowLayout {
            anchors.fill: parent
            spacing: 0

            // 左侧导航栏（完全保留代码1结构）
            Rectangle {
                id: leftNav
                width: 220
                color: "#4292c7"
                Layout.fillHeight: true

                ColumnLayout {
                    anchors.fill: parent
                    spacing: 0

                    // 导航按钮组（完全保留代码1结构）
                    Repeater {
                        id: navButtons
                        model: [
                            { name: "操作首页", icon: "🏠" },
                            { name: "远程控制", icon: "🎮" },
                            { name: "算法模块", icon: "🧠" },
                            { name: "GIS 模块", icon: "🗺️" },
                            { name: "通讯模块", icon: "📡" },
                            { name: "信息管理模块", icon: "📊" },
                            { name: "系统服务", icon: "⚙️" }
                        ]

                        Button {
                            property bool isSelected: modelData.name === currentPage
                            property string pageName: modelData.name
                            property string iconText: modelData.icon
                            property string buttonText: modelData.name

                            Layout.fillWidth: true
                            Layout.preferredHeight: 55

                            background: Rectangle {
                                color: isSelected ? primaryColor : "transparent"
                                border.color: isSelected ? "#2980b9" : "transparent"
                                border.width: isSelected ? 2 : 0
                                radius: 6
                            }

                            contentItem: RowLayout {
                                spacing: 10
                                anchors.centerIn: parent

                                Text {
                                    text: iconText
                                    font.pixelSize: 16
                                    color: "white"
                                }

                                Text {
                                    text: buttonText
                                    color: "#000000"
                                    font.pixelSize: 14
                                    font.bold: isSelected
                                }
                            }

                            onClicked: {
                                switchPage(pageName)
                            }
                        }
                    }

                    // 占位（完全保留代码1结构）
                    Item { Layout.fillHeight: true }

                    // 机器人管理区域（优化宽度控制，确保不超出导航栏）
                    Rectangle {
                        color: secondaryColor
                        Layout.fillWidth: true
                        Layout.preferredHeight: showRobotList ? 280 : 120
                        Layout.leftMargin: 8     // 减小左边距
                        Layout.rightMargin: 8    // 减小右边距
                        Layout.bottomMargin: 8   // 减小下边距
                        radius: borderRadius

                        Behavior on Layout.preferredHeight {
                            NumberAnimation {
                                duration: 200
                                easing.type: Easing.OutCubic
                            }
                        }

                        ColumnLayout {
                            anchors.fill: parent
                            anchors.margins: 10      // 减小内部边距
                            spacing: 10              // 减小内部间距

                            // 标题栏（优化宽度控制，防止超出导航栏）
                            RowLayout {
                                Layout.fillWidth: true
                                Layout.margins: 0  // 确保没有额外边距
                                spacing: 6         // 进一步减小间距

                                Label {
                                    text: "机器人管理"
                                    color: "black"
                                    font.bold: true
                                    font.pixelSize: 13      // 稍微减小字体
                                    Layout.preferredWidth: 70   // 进一步减小标题宽度
                                    Layout.maximumWidth: 70     // 设置最大宽度限制
                                    elide: Text.ElideRight     // 文本过长时显示省略号
                                    clip: true                  // 裁剪超出的内容
                                }
                                
                                // 占位符，推动右侧元素
                                Item {
                                    Layout.fillWidth: true
                                    Layout.minimumWidth: 5     // 减小最小间距
                                }
                                
                                // TCP服务器状态指示器
                                Rectangle {
                                    width: 6                    // 减小指示器尺寸
                                    height: 6
                                    radius: 3
                                    color: (typeof robotManager !== 'undefined' && robotManager.serverRunning) ? "#27ae60" : "#e74c3c"
                                    Layout.alignment: Qt.AlignVCenter
                                    
                                    ToolTip {
                                        text: (typeof robotManager !== 'undefined' && robotManager.serverRunning) ? 
                                              "TCP服务器运行中 (" + robotManager.serverPort + ")" : 
                                              "TCP服务器已停止"
                                        visible: parent.hovered
                                    }
                                    
                                    property bool hovered: false
                                    MouseArea {
                                        anchors.fill: parent
                                        hoverEnabled: true
                                        onEntered: parent.hovered = true
                                        onExited: parent.hovered = false
                                    }
                                }
                                
                                // 刷新按钮
                                Button {
                                    text: "刷新"
                                    background: Rectangle {
                                        color: "#f39c12"
                                        radius: smallBorderRadius
                                    }
                                    contentItem: Text {
                                        text: parent.text
                                        color: "black"
                                        font.pixelSize: 9        // 进一步减小字体
                                        horizontalAlignment: Text.AlignHCenter
                                        verticalAlignment: Text.AlignVCenter
                                    }
                                    Layout.preferredWidth: 32    // 进一步减小按钮宽度
                                    Layout.maximumWidth: 32      // 设置最大宽度限制
                                    Layout.preferredHeight: 20   // 减小按钮高度
                                    onClicked: {
                                        refreshRobotList()
                                    }
                                }

                                Button {
                                    text: showRobotList ? "折叠" : "列表"
                                    background: Rectangle {
                                        color: primaryColor
                                        radius: smallBorderRadius
                                    }
                                    contentItem: Text {
                                        text: parent.text
                                        color: "black"
                                        font.pixelSize: 9        // 进一步减小字体
                                        horizontalAlignment: Text.AlignHCenter
                                        verticalAlignment: Text.AlignVCenter
                                    }
                                    Layout.preferredWidth: 32    // 进一步减小按钮宽度
                                    Layout.maximumWidth: 32      // 设置最大宽度限制
                                    Layout.preferredHeight: 20   // 减小按钮高度
                                    onClicked: {
                                        showRobotList = !showRobotList
                                    }
                                }
                            }

                            // 当前选中的机器人（显示数据库信息）
                            Rectangle {
                                Layout.fillWidth: true
                                Layout.preferredHeight: 35
                                color: primaryColor
                                radius: 6
                                border.color: "#2980b9"
                                border.width: 1

                                RowLayout {
                                    anchors.fill: parent
                                    anchors.margins: 8
                                    spacing: 8

                                    Rectangle {
                                        width: 8
                                        height: 8
                                        radius: 4
                                        color: "black"
                                    }

                                    Text {
                                        text: "当前：" + (currentRobot || "无")
                                        color: "black"
                                        font.pixelSize: 12
                                        font.bold: true
                                        Layout.fillWidth: true
                                    }

                                    Rectangle {
                                        width: 6
                                        height: 6
                                        radius: 3
                                        color: getRobotOnlineStatus(currentRobot) ? "#27ae60" : "#95a5a6"
                                    }
                                    
                                    // 显示连接数量
                                    Text {
                                        text: typeof robotManager !== 'undefined' ? 
                                              "(" + robotManager.connectedRobotsCount + "/" + robotList.length + ")" : 
                                              ""
                                        color: "black"
                                        font.pixelSize: 10
                                        opacity: 0.8
                                    }
                                }
                            }

                            // 机器人列表（完全保留代码1的动态Repeater+折叠功能）
                            Rectangle {
                                Layout.fillWidth: true
                                Layout.fillHeight: true
                                color: "transparent"
                                visible: showRobotList

                                ScrollView {
                                    anchors.fill: parent
                                    clip: true

                                    ColumnLayout {
                                        width: parent.width
                                        spacing: 6

                                        Repeater {
                                            model: robotList

                                            Rectangle {
                                                property bool isCurrent: (modelData.sn || modelData.name) === currentRobot
                                                property bool isOnline: modelData.online || modelData.isOnline  // 兼容两种字段名
                                                property string robotSN: modelData.sn || modelData.name
                                                property string robotIP: modelData.ip || ""

                                                Layout.fillWidth: true
                                                Layout.preferredHeight: 32
                                                color: isCurrent ? primaryColor : (isOnline ? successColor : "#95a5a6")
                                                radius: 4
                                                opacity: isCurrent ? 1.0 : (isOnline ? 0.8 : 0.6)

                                                RowLayout {
                                                    anchors.fill: parent
                                                    anchors.margins: 6
                                                    spacing: 6

                                                    Rectangle {
                                                        width: 6
                                                        height: 6
                                                        radius: 3
                                                        color: "white"
                                                    }

                                                    Text {
                                                        text: robotSN
                                                        color: "white"
                                                        font.pixelSize: 11
                                                        font.bold: isCurrent
                                                        Layout.fillWidth: true
                                                    }
                                                    
                                                    // 显示实时状态信息（适配内存优先架构）
                                                    Text {
                                                        text: modelData.temperature ? modelData.temperature + "°C" : ""
                                                        color: "white"
                                                        font.pixelSize: 8
                                                        opacity: 0.8
                                                        visible: modelData.temperature > 0
                                                    }
                                                    
                                                    Text {
                                                        text: modelData.battery ? modelData.battery + "%" : ""
                                                        color: modelData.battery > 20 ? "white" : "#ff6b6b"
                                                        font.pixelSize: 8
                                                        opacity: 0.8
                                                        visible: modelData.battery > 0
                                                    }

                                                    Text {
                                                        text: isOnline ? "在线" : "离线"
                                                        color: "white"
                                                        font.pixelSize: 9
                                                        opacity: 0.8
                                                    }
                                                }

                                                // 悬停提示和点击事件
                                                MouseArea {
                                                    id: mouseArea
                                                    anchors.fill: parent
                                                    hoverEnabled: true
                                                    onClicked: {
                                                        if (robotSN !== currentRobot) {
                                                            switchRobot(robotSN)
                                                        }
                                                    }
                                                }
                                                
                                                ToolTip {
                                                    text: "SN: " + robotSN + 
                                                          (robotIP ? "\nIP: " + robotIP : "") +
                                                          "\n状态: " + (isOnline ? "在线" : "离线") +
                                                          (modelData.last_ping ? "\n最后心跳: " + modelData.last_ping : "") +
                                                          (modelData.temperature ? "\n温度: " + modelData.temperature + "°C" : "") +
                                                          (modelData.battery ? "\n电量: " + modelData.battery + "%" : "") +
                                                          (modelData.status ? "\n状态: " + modelData.status : "")
                                                    visible: mouseArea.containsMouse
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // 中间内容区域（完全保留代码1结构）
            Rectangle {
                id: centerContent
                color: cardBackground
                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.margins: 10
                radius: borderRadius

                DropShadow {
                    anchors.fill: parent
                    source: parent
                    horizontalOffset: 0
                    verticalOffset: 2
                    radius: shadowRadius
                    samples: 16
                    color: "#20000000"
                }

                // 使用多个Loader实现组件缓存，每个页面一个Loader
                // 通过visible控制显示，组件永不销毁，状态自然保持
                
                // 根据用户权限加载不同的主页
                Loader {
                    id: homePageLoader
                    anchors.fill: parent
                    anchors.margins: 20
                    source: (typeof authManager !== 'undefined' && authManager.isAdmin) ? 
                            "qrc:/pages/HomePage.qml" : "qrc:/pages/UserHomePage.qml"
                    active: true
                    visible: currentPage === "操作首页"
                    asynchronous: true
                }
                
                Loader {
                    id: remoteControlPageLoader
                    anchors.fill: parent
                    anchors.margins: 20
                    source: "qrc:/pages/RemoteControlPage.qml"
                    active: true
                    visible: currentPage === "远程控制"
                    asynchronous: true
                    
                    // 监听可见性变化，当页面变为可见时调用onPageActivated函数
                    onVisibleChanged: {
                        if (visible && item && typeof item.onPageActivated === "function") {
                            item.onPageActivated()
                        }
                    }
                }
                
                Loader {
                    id: algorithmPageLoader
                    anchors.fill: parent
                    anchors.margins: 20
                    source: "qrc:/pages/AlgorithmPage.qml"
                    active: true
                    visible: currentPage === "算法模块"
                    asynchronous: true
                }
                
                Loader {
                    id: gisPageLoader
                    anchors.fill: parent
                    anchors.margins: 20
                    source: "qrc:/pages/GISPage.qml"
                    active: true
                    visible: currentPage === "GIS 模块"
                    asynchronous: true
                    
                    onStatusChanged: {
                        if (status == Loader.Ready) {
                            console.log("✅ GIS页面已加载并缓存，组件不会被销毁")
                        } else if (status == Loader.Error) {
                            console.error("GIS页面加载错误: Loader状态异常")
                        }
                    }
                }
                
                Loader {
                    id: communicationPageLoader
                    anchors.fill: parent
                    anchors.margins: 20
                    source: "qrc:/pages/CommunicationPage.qml"
                    active: true
                    visible: currentPage === "通讯模块"
                    asynchronous: true
                }
                
                Loader {
                    id: infoManagementPageLoader
                    anchors.fill: parent
                    anchors.margins: 20
                    source: "qrc:/pages/InfoManagementPage.qml"
                    active: true
                    visible: currentPage === "信息管理模块"
                    asynchronous: true
                }
                
                Loader {
                    id: systemServicePageLoader
                    anchors.fill: parent
                    anchors.margins: 20
                    source: "qrc:/pages/SystemServicePage.qml"
                    active: true
                    visible: currentPage === "系统服务"
                    asynchronous: true
                }
            }

            // 右侧机器人状态信息（完全保留代码1结构）
            Rectangle {
                id: rightStatus
                width: 320
                color: "#ecf0f1"
                Layout.fillHeight: true
                Layout.margins: 10
                radius: borderRadius

                DropShadow {
                    anchors.fill: parent
                    source: parent
                    horizontalOffset: 0
                    verticalOffset: 2
                    radius: shadowRadius
                    samples: 16
                    color: "#20000000"
                }

                ScrollView {
                    anchors.fill: parent
                    anchors.margins: 15
                    clip: true

                    ColumnLayout {
                        width: parent.width
                        spacing: 15

                        Rectangle {
                            visible: isCurrentRobotOffline
                            color: "#7f8c8d"
                            radius: borderRadius
                            Layout.fillWidth: true
                            Layout.preferredHeight: 120

                            ColumnLayout {
                                anchors.centerIn: parent
                                spacing: 15

                                Rectangle {
                                    width: 60
                                    height: 60
                                    radius: 30
                                    color: "white"
                                    Layout.alignment: Qt.AlignHCenter

                                    Label {
                                        text: "⚠️"
                                        font.pixelSize: 32
                                        anchors.centerIn: parent
                                    }
                                }

                                Label {
                                    text: "机器人 " + currentRobot + " 已离线"
                                    font.pixelSize: 18
                                    font.bold: true
                                    color: "white"
                                    Layout.alignment: Qt.AlignHCenter
                                }

                                Label {
                                    text: "无法获取机器人状态信息"
                                    font.pixelSize: 12
                                    color: "white"
                                    opacity: 0.8
                                    Layout.alignment: Qt.AlignHCenter
                                }
                            }
                        }

                        // 基本标识与系统状态（始终显示，包含离线状态）
                        Rectangle {
                            color: cardBackground
                            radius: borderRadius
                            Layout.fillWidth: true
                            Layout.preferredHeight: 90

                            DropShadow {
                                anchors.fill: parent
                                source: parent
                                horizontalOffset: 0
                                verticalOffset: 1
                                radius: 4
                                samples: 8
                                color: "#10000000"
                            }

                            ColumnLayout {
                                anchors.fill: parent
                                spacing: 0

                                Rectangle {
                                    color: "#f8f9fa"
                                    Layout.fillWidth: true
                                    Layout.preferredHeight: 35
                                    radius: 8

                                    Label {
                                        text: "机器人 " + currentRobot + (isCurrentRobotOffline ? " (离线)" : " (在线)")
                                        font.pixelSize: 14
                                        font.bold: true
                                        color: isCurrentRobotOffline ? "#7f8c8d" : textColor
                                        anchors.left: parent.left
                                        anchors.leftMargin: cardMargin
                                        anchors.verticalCenter: parent.verticalCenter
                                    }
                                }

                                RowLayout {
                                    Layout.fillWidth: true
                                    Layout.fillHeight: true
                                    anchors.margins: 15
                                    spacing: 10

                                    ColumnLayout {
                                        Layout.fillWidth: true
                                        spacing: 5

                                        RowLayout {
                                            spacing: 8

                                            Label {
                                                text: "系统状态"
                                                color: textColor
                                                font.pixelSize: 12
                                            }

                                            Rectangle {
                                                width: 8
                                                height: 8
                                                radius: 4
                                                color: isCurrentRobotOffline ? "#7f8c8d" : successColor
                                            }
                                            
                                            Label {
                                                text: isCurrentRobotOffline ? "离线" : "在线"
                                                color: isCurrentRobotOffline ? "#7f8c8d" : successColor
                                                font.pixelSize: 11
                                                font.bold: true
                                            }

                                        }
                                    }

                                    Button {
                                        text: "刷新"
                                        background: Rectangle {
                                            color: primaryColor
                                            radius: 6
                                        }
                                        contentItem: Text {
                                            text: "刷新"
                                            color: "black"
                                            font.pixelSize: 12
                                            horizontalAlignment: Text.AlignHCenter
                                        }
                                        Layout.preferredWidth: 60
                                        Layout.preferredHeight: 30
                                        onClicked: updateRobotStatus()
                                    }
                                }
                            }
                        }



                        // 传感器数据（始终显示，但内容根据状态调整）
                        Rectangle {
                            color: "white"
                            radius: 8
                            Layout.fillWidth: true
                            Layout.preferredHeight: 180

                            DropShadow {
                                anchors.fill: parent
                                source: parent
                                horizontalOffset: 0
                                verticalOffset: 1
                                radius: 4
                                samples: 8
                                color: "#10000000"
                            }

                            ColumnLayout {
                                anchors.fill: parent
                                spacing: 0

                                Rectangle {
                                    color: "#f8f9fa"
                                    Layout.fillWidth: true
                                    Layout.preferredHeight: 35
                                    radius: 8

                                    Label {
                                        text: "传感器数据"
                                        font.pixelSize: 14
                                        font.bold: true
                                        color: "#2c3e50"
                                        anchors.left: parent.left
                                        anchors.leftMargin: 15
                                        anchors.verticalCenter: parent.verticalCenter
                                    }
                                }

                                ColumnLayout {
                                    Layout.fillWidth: true
                                    Layout.fillHeight: true
                                    anchors.margins: 15
                                    spacing: 8
                                    
                                    // 机器人在线时显示实际传感器数据
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 8
                                        visible: !isCurrentRobotOffline

                                        Repeater {
                                            model: [
                                                { label: "激光测距 1", value: "正常", status: "normal" },
                                                { label: "激光测距 2", value: "正常", status: "normal" },
                                                { label: "红外传感器 1", value: "正常", status: "normal" },
                                                { label: "红外传感器 2", value: "正常", status: "normal" },
                                                { label: "环境传感器 CAN", value: "温度：42℃ 湿度：- 气压：-", status: "warning" }
                                            ]

                                            RowLayout {
                                                Layout.fillWidth: true
                                                Layout.preferredHeight: 25
                                                spacing: 10

                                                Label {
                                                    text: modelData.label + ":"
                                                    color: "#34495e"
                                                    font.pixelSize: 12
                                                    width: 80
                                                }

                                                Rectangle {
                                                    width: 6
                                                    height: 6
                                                    radius: 3
                                                    color: {
                                                        switch(modelData.status) {
                                                            case "normal": return "#27ae60"
                                                            case "warning": return "#f39c12"
                                                            case "error": return "#e74c3c"
                                                            case "info": return "#3498db"
                                                            default: return "#95a5a6"
                                                        }
                                                    }
                                                }

                                                Label {
                                                    text: modelData.value
                                                    color: "#2c3e50"
                                                    font.pixelSize: 12
                                                    font.bold: modelData.status === "normal"
                                                    Layout.fillWidth: true
                                                }
                                            }
                                        }
                                    }
                                    
                                    // 机器人离线时显示的内容
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 10
                                        visible: isCurrentRobotOffline
                                        
                                        RowLayout {
                                            Layout.fillWidth: true
                                            spacing: 10
                                            
                                            Rectangle {
                                                width: 16
                                                height: 16
                                                radius: 8
                                                color: errorColor
                                                
                                                Label {
                                                    text: "!"
                                                    color: "white"
                                                    font.pixelSize: 10
                                                    font.bold: true
                                                    anchors.centerIn: parent
                                                }
                                            }
                                            
                                            Label {
                                                text: "机器人已离线，无法获取传感器数据"
                                                color: "#7f8c8d"
                                                font.pixelSize: 12
                                                font.italic: true
                                                Layout.fillWidth: true
                                            }
                                        }
                                        
                                        // 显示传感器离线状态
                                        Repeater {
                                            model: [
                                                "激光测距 1", "激光测距 2", "红外传感器 1", 
                                                "红外传感器 2", "环境传感器 CAN"
                                            ]
                                            
                                            RowLayout {
                                                Layout.fillWidth: true
                                                Layout.preferredHeight: 20
                                                spacing: 10
                                                
                                                Label {
                                                    text: modelData + ":"
                                                    color: "#7f8c8d"
                                                    font.pixelSize: 11
                                                    width: 80
                                                }
                                                
                                                Rectangle {
                                                    width: 6
                                                    height: 6
                                                    radius: 3
                                                    color: "#95a5a6"
                                                }
                                                
                                                Label {
                                                    text: "离线"
                                                    color: "#95a5a6"
                                                    font.pixelSize: 11
                                                    Layout.fillWidth: true
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // 设备硬件状态（始终显示，但内容根据状态调整）
                        Rectangle {
                            color: "white"
                            radius: 8
                            Layout.fillWidth: true
                            Layout.preferredHeight: 140

                            DropShadow {
                                anchors.fill: parent
                                source: parent
                                horizontalOffset: 0
                                verticalOffset: 1
                                radius: 4
                                samples: 8
                                color: "#10000000"
                            }

                            ColumnLayout {
                                anchors.fill: parent
                                spacing: 0

                                Rectangle {
                                    color: "#f8f9fa"
                                    Layout.fillWidth: true
                                    Layout.preferredHeight: 35
                                    radius: 8

                                    Label {
                                        text: "设备硬件状态（PLC 层）"
                                        font.pixelSize: 14
                                        font.bold: true
                                        color: "#2c3e50"
                                        anchors.left: parent.left
                                        anchors.leftMargin: 15
                                        anchors.verticalCenter: parent.verticalCenter
                                    }
                                }

                                ColumnLayout {
                                    Layout.fillWidth: true
                                    Layout.fillHeight: true
                                    anchors.margins: 15
                                    spacing: 8
                                    
                                    // 机器人在线时显示实际硬件状态
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 8
                                        visible: !isCurrentRobotOffline

                                        Repeater {
                                            model: [
                                                { label: "左电机", value: "正常", status: "normal" },
                                                { label: "右电机", value: "正常", status: "normal" },
                                                { label: "左刀具", value: "正常", status: "normal" },
                                                { label: "右刀具", value: "正常", status: "normal" }
                                            ]

                                            RowLayout {
                                                Layout.fillWidth: true
                                                Layout.preferredHeight: 25
                                                spacing: 10

                                                Label {
                                                    text: modelData.label + ":"
                                                    color: "#34495e"
                                                    font.pixelSize: 12
                                                    width: 80
                                                }

                                                Rectangle {
                                                    width: 6
                                                    height: 6
                                                    radius: 3
                                                    color: "#27ae60"
                                                }

                                                Label {
                                                    text: modelData.value
                                                    color: "#2c3e50"
                                                    font.pixelSize: 12
                                                    font.bold: true
                                                    Layout.fillWidth: true
                                                }
                                            }
                                        }
                                    }
                                    
                                    // 机器人离线时显示的内容
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 10
                                        visible: isCurrentRobotOffline
                                        
                                        RowLayout {
                                            Layout.fillWidth: true
                                            spacing: 10
                                            
                                            Rectangle {
                                                width: 16
                                                height: 16
                                                radius: 8
                                                color: errorColor
                                                
                                                Label {
                                                    text: "!"
                                                    color: "white"
                                                    font.pixelSize: 10
                                                    font.bold: true
                                                    anchors.centerIn: parent
                                                }
                                            }
                                            
                                            Label {
                                                text: "机器人已离线，无法获取硬件状态"
                                                color: "#7f8c8d"
                                                font.pixelSize: 12
                                                font.italic: true
                                                Layout.fillWidth: true
                                            }
                                        }
                                        
                                        // 显示硬件离线状态
                                        Repeater {
                                            model: ["左电机", "右电机", "左刀具", "右刀具"]
                                            
                                            RowLayout {
                                                Layout.fillWidth: true
                                                Layout.preferredHeight: 20
                                                spacing: 10
                                                
                                                Label {
                                                    text: modelData + ":"
                                                    color: "#7f8c8d"
                                                    font.pixelSize: 11
                                                    width: 80
                                                }
                                                
                                                Rectangle {
                                                    width: 6
                                                    height: 6
                                                    radius: 3
                                                    color: "#95a5a6"
                                                }
                                                
                                                Label {
                                                    text: "无状态"
                                                    color: "#95a5a6"
                                                    font.pixelSize: 11
                                                    Layout.fillWidth: true
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // 通信状态（始终显示，但内容根据状态调整）
                        Rectangle {
                            color: "white"
                            radius: 8
                            Layout.fillWidth: true
                            Layout.preferredHeight: 100

                            DropShadow {
                                anchors.fill: parent
                                source: parent
                                horizontalOffset: 0
                                verticalOffset: 1
                                radius: 4
                                samples: 8
                                color: "#10000000"
                            }

                            ColumnLayout {
                                anchors.fill: parent
                                spacing: 0

                                Rectangle {
                                    color: "#f8f9fa"
                                    Layout.fillWidth: true
                                    Layout.preferredHeight: 35
                                    radius: 8

                                    Label {
                                        text: "通信状态"
                                        font.pixelSize: 14
                                        font.bold: true
                                        color: "#2c3e50"
                                        anchors.left: parent.left
                                        anchors.leftMargin: 15
                                        anchors.verticalCenter: parent.verticalCenter
                                    }
                                }

                                ColumnLayout {
                                    Layout.fillWidth: true
                                    Layout.fillHeight: true
                                    anchors.margins: 15
                                    spacing: 8
                                    
                                    // 机器人在线时显示实际通信状态
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 8
                                        visible: !isCurrentRobotOffline

                                        Repeater {
                                            model: [
                                                { label: "连接状态", value: "连接", status: "normal" },
                                                { label: "协议状态", value: "TCP/IP 正常 Modbus 正常", status: "normal" }
                                            ]

                                            RowLayout {
                                                Layout.fillWidth: true
                                                Layout.preferredHeight: 25
                                                spacing: 10

                                                Label {
                                                    text: modelData.label + ":"
                                                    color: "#34495e"
                                                    font.pixelSize: 12
                                                    width: 80
                                                }

                                                Rectangle {
                                                    width: 6
                                                    height: 6
                                                    radius: 3
                                                    color: "#27ae60"
                                                }

                                                Label {
                                                    text: modelData.value
                                                    color: "#2c3e50"
                                                    font.pixelSize: 12
                                                    font.bold: true
                                                    Layout.fillWidth: true
                                                }
                                            }
                                        }
                                    }
                                    
                                    // 机器人离线时显示的内容
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 10
                                        visible: isCurrentRobotOffline
                                        
                                        RowLayout {
                                            Layout.fillWidth: true
                                            spacing: 10
                                            
                                            Rectangle {
                                                width: 16
                                                height: 16
                                                radius: 8
                                                color: errorColor
                                                
                                                Label {
                                                    text: "!"
                                                    color: "white"
                                                    font.pixelSize: 10
                                                    font.bold: true
                                                    anchors.centerIn: parent
                                                }
                                            }
                                            
                                            Label {
                                                text: "机器人已离线，通信中断"
                                                color: "#7f8c8d"
                                                font.pixelSize: 12
                                                font.italic: true
                                                Layout.fillWidth: true
                                            }
                                        }
                                        
                                        // 显示通信离线状态
                                        RowLayout {
                                            Layout.fillWidth: true
                                            Layout.preferredHeight: 20
                                            spacing: 10
                                            
                                            Label {
                                                text: "连接状态:"
                                                color: "#7f8c8d"
                                                font.pixelSize: 11
                                                width: 80
                                            }
                                            
                                            Rectangle {
                                                width: 6
                                                height: 6
                                                radius: 3
                                                color: errorColor
                                            }
                                            
                                            Label {
                                                text: "已断开"
                                                color: errorColor
                                                font.pixelSize: 11
                                                font.bold: true
                                                Layout.fillWidth: true
                                            }
                                        }
                                        
                                        RowLayout {
                                            Layout.fillWidth: true
                                            Layout.preferredHeight: 20
                                            spacing: 10
                                            
                                            Label {
                                                text: "协议状态:"
                                                color: "#7f8c8d"
                                                font.pixelSize: 11
                                                width: 80
                                            }
                                            
                                            Rectangle {
                                                width: 6
                                                height: 6
                                                radius: 3
                                                color: "#95a5a6"
                                            }
                                            
                                            Label {
                                                text: "无法获取"
                                                color: "#95a5a6"
                                                font.pixelSize: 11
                                                Layout.fillWidth: true
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // 任务详情（始终显示，但内容根据状态调整）
                        Rectangle {
                            color: "white"
                            radius: 8
                            Layout.fillWidth: true
                            Layout.preferredHeight: 120

                            DropShadow {
                                anchors.fill: parent
                                source: parent
                                horizontalOffset: 0
                                verticalOffset: 1
                                radius: 4
                                samples: 8
                                color: "#10000000"
                            }

                            ColumnLayout {
                                anchors.fill: parent
                                spacing: 0

                                Rectangle {
                                    color: "#f8f9fa"
                                    Layout.fillWidth: true
                                    Layout.preferredHeight: 35
                                    radius: 8

                                    Label {
                                        text: "任务详情"
                                        font.pixelSize: 14
                                        font.bold: true
                                        color: "#2c3e50"
                                        anchors.left: parent.left
                                        anchors.leftMargin: 15
                                        anchors.verticalCenter: parent.verticalCenter
                                    }
                                }

                                ColumnLayout {
                                    Layout.fillWidth: true
                                    Layout.fillHeight: true
                                    anchors.margins: 15
                                    spacing: 10
                                    
                                    // 机器人在线时显示实际任务信息
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 10
                                        visible: !isCurrentRobotOffline

                                        RowLayout {
                                            Layout.fillWidth: true
                                            Layout.preferredHeight: 25
                                            spacing: 10

                                            Label {
                                                text: "当前任务:"
                                                color: "#34495e"
                                                font.pixelSize: 12
                                                width: 80
                                            }

                                            Rectangle {
                                                width: 6
                                                height: 6
                                                radius: 3
                                                color: "#3498db"
                                            }

                                            Label {
                                                text: "待机中"
                                                color: "#2c3e50"
                                                font.pixelSize: 12
                                                font.bold: true
                                                Layout.fillWidth: true
                                            }
                                        }

                                        ColumnLayout {
                                            Layout.fillWidth: true
                                            Layout.preferredHeight: 40
                                            spacing: 5

                                            RowLayout {
                                                Layout.fillWidth: true
                                                spacing: 10

                                                Label {
                                                    text: "完成进度:"
                                                    color: "#34495e"
                                                    font.pixelSize: 12
                                                    width: 80
                                                }

                                                Label {
                                                    text: "50%"
                                                    color: "#2c3e50"
                                                    font.pixelSize: 12
                                                    font.bold: true
                                                    Layout.fillWidth: true
                                                }
                                            }

                                            Rectangle {
                                                color: "#ecf0f1"
                                                Layout.fillWidth: true
                                                height: 6
                                                radius: 3

                                                Rectangle {
                                                    color: "#27ae60"
                                                    width: parent.width * 0.5
                                                    height: parent.height
                                                    radius: 3
                                                }
                                            }
                                        }

                                        RowLayout {
                                            Layout.fillWidth: true
                                            Layout.preferredHeight: 25
                                            spacing: 10

                                            Label {
                                                text: "工作时间:"
                                                color: "#34495e"
                                                font.pixelSize: 12
                                                width: 80
                                            }

                                            Rectangle {
                                                width: 6
                                                height: 6
                                                radius: 3
                                                color: "#3498db"
                                            }

                                            Label {
                                                text: "--"
                                                color: "#2c3e50"
                                                font.pixelSize: 12
                                                font.bold: true
                                                Layout.fillWidth: true
                                            }
                                        }
                                    }
                                    
                                    // 机器人离线时显示的内容
                                    Column {
                                        Layout.fillWidth: true
                                        spacing: 10
                                        visible: isCurrentRobotOffline
                                        
                                        RowLayout {
                                            Layout.fillWidth: true
                                            spacing: 10
                                            
                                            Rectangle {
                                                width: 16
                                                height: 16
                                                radius: 8
                                                color: errorColor
                                                
                                                Label {
                                                    text: "!"
                                                    color: "white"
                                                    font.pixelSize: 10
                                                    font.bold: true
                                                    anchors.centerIn: parent
                                                }
                                            }
                                            
                                            Label {
                                                text: "机器人已离线，任务执行中断"
                                                color: "#7f8c8d"
                                                font.pixelSize: 12
                                                font.italic: true
                                                Layout.fillWidth: true
                                            }
                                        }
                                        
                                        // 显示任务离线状态
                                        RowLayout {
                                            Layout.fillWidth: true
                                            Layout.preferredHeight: 20
                                            spacing: 10
                                            
                                            Label {
                                                text: "任务状态:"
                                                color: "#7f8c8d"
                                                font.pixelSize: 11
                                                width: 80
                                            }
                                            
                                            Rectangle {
                                                width: 6
                                                height: 6
                                                radius: 3
                                                color: errorColor
                                            }
                                            
                                            Label {
                                                text: "已暂停"
                                                color: errorColor
                                                font.pixelSize: 11
                                                font.bold: true
                                                Layout.fillWidth: true
                                            }
                                        }
                                        
                                        RowLayout {
                                            Layout.fillWidth: true
                                            Layout.preferredHeight: 20
                                            spacing: 10
                                            
                                            Label {
                                                text: "数据同步:"
                                                color: "#7f8c8d"
                                                font.pixelSize: 11
                                                width: 80
                                            }
                                            
                                            Rectangle {
                                                width: 6
                                                height: 6
                                                radius: 3
                                                color: "#95a5a6"
                                            }
                                            
                                            Label {
                                                text: "等待机器人上线后恢复"
                                                color: "#95a5a6"
                                                font.pixelSize: 11
                                                Layout.fillWidth: true
                                                wrapMode: Text.WordWrap
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // 更新机器人状态信息（集成数据库刷新）
    function updateRobotStatus() {
        console.log("刷新机器人状态信息：" + currentRobot)
            
        // 刷新机器人列表
        refreshRobotList()
            
        // 如果有具体的机器人，获取详细信息
        if (currentRobot && typeof robotManager !== 'undefined') {
            try {
                var robotInfo = robotManager.getRobotInfo(currentRobot)
                if (robotInfo.sn) {
                    console.log("机器人详细信息:", JSON.stringify(robotInfo))
                }
            } catch (error) {
                console.log("获取机器人详细信息失败:", error)
            }
        }
    }

    // 获取机器人在线状态（适配数据库结构，带安全检查）
    function getRobotOnlineStatus(robotName) {
        if (!robotName || !robotList || robotList.length === 0) {
            return false
        }
            
        for (var i = 0; i < robotList.length; i++) {
            if (robotList[i].name === robotName || robotList[i].sn === robotName) {
                return robotList[i].online || robotList[i].isOnline  // 兼容两种字段名
            }
        }
        return false
    }
}
